import matplotlib.pyplot as plt

if __name__ == "__main__":
    # 创建画布和坐标轴
    fig, ax = plt.subplots(figsize=(8, 6))

    # 绘制向量v（蓝色箭头）
    ax.quiver(0, 0, 4, 2,
              angles='xy', scale_units='xy', scale=1,
              color='blue', width=0.02, headwidth=5, label='v = (4, 2)')

    # 绘制向量w（绿色箭头）
    ax.quiver(0, 0, -1, 2,
              angles='xy', scale_units='xy', scale=1,
              color='green', width=0.02, headwidth=5, label='w = (-1, 2)')


    # 设置坐标轴范围和比例
    ax.set_xlim(-2, 5)
    ax.set_ylim(-1, 5)
    ax.set_aspect('equal')  # 确保坐标轴比例一致

    # 绘制辅助虚线（向量叠加过程）
    ax.plot([4, 3], [2, 4], 'k--', alpha=0.5)  # 从v到v+w
    ax.plot([-1, 3], [2, 4], 'k--', alpha=0.5)  # 从w到v+w

    # 绘制向量和v + w（红色箭头）
    ax.quiver(0, 0, 3, 4,
              angles='xy', scale_units='xy', scale=1,
              color='red', width=0.02, headwidth=5, label='v + w = (3, 4)')

    # 绘制原点和标签
    ax.scatter(0, 0, color='black', s=50, zorder=3, label='Origin (0,0)')
    ax.set_xlabel("X-axis")
    ax.set_ylabel("Y-axis")
    ax.set_title("Vector Addition: v + w")

    # 显示网格和图例
    ax.grid(True, linestyle='--', alpha=0.5)
    
    ax.legend()

    plt.show()
